package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.LoginRequired;
import com.alili_tv.system.common.R;
import com.alili_tv.system.entity.CollectFolder;
import com.alili_tv.system.entity.User;
import com.alili_tv.system.entity.Video;
import com.alili_tv.system.service.CollectFolderService;
import com.alili_tv.system.service.UserService;
import com.alili_tv.system.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping(value = "/collectFolder", produces = "application/json")
public class CollectFolderController {

    @Resource
    private CollectFolderService collectFolderService;

    @Resource
    private VideoService videoService;

    @Resource
    private UserService userService;

    /**
     * 添加，删除收藏夹
     *
     * @param videoId
     * @param isAdd
     * @return
     */
    @LoginRequired
    @PostMapping("/{videoId}/{isAdd}")
    public R<String> add(@PathVariable Long videoId, @PathVariable Boolean isAdd) {
        if (isAdd) {
            CollectFolder folder = new CollectFolder();
            folder.setVideoId(videoId);
            collectFolderService.save(folder);
        } else {
            LambdaQueryWrapper<CollectFolder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(CollectFolder::getCreateUser, BaseContext.getCurrentId())
                    .eq(CollectFolder::getVideoId, videoId);
            collectFolderService.remove(queryWrapper);
        }
        return R.success("success");
    }

    @GetMapping("/{userId}")
    public R<Page<Video>> page(@PathVariable Long userId, Integer pageIndex, Integer pageSize) {
        Page<CollectFolder> page = new Page<>(pageIndex, pageSize);

        // 查询用户收藏的视频id
        LambdaQueryWrapper<CollectFolder> queryWrapper = new LambdaQueryWrapper<CollectFolder>()
                .eq(CollectFolder::getCreateUser, userId)
                .orderByDesc(CollectFolder::getCreateTime);
        collectFolderService.page(page, queryWrapper);

        Page<Video> videoPage = new Page<>(pageIndex, pageSize);
        videoPage.setTotal(page.getTotal());

        // 若查询条数为0，无需继续以下操作
        if (videoPage.getTotal() == 0) {
            return R.success(videoPage);
        }

        // 获取ids
        List<Long> videoIds = page.getRecords().stream().map(CollectFolder::getVideoId).toList();

        // 查询视频
        Map<Long, Video> videosMap = videoService.getByIds(videoIds);

        List<Video> resultVideos = page.getRecords().stream().map(item -> {
            Video video = videosMap.get(item.getVideoId());
            return video != null ? video : new Video();
        }).toList();


        videoPage.setRecords(resultVideos);

        return R.success(videoPage);
    }
}
